// TypeScript file
class MyButtonForGame extends egret.Sprite
{
    private bg: egret.Bitmap;
    private title: egret.Bitmap;
    private onClick: Function;
    private tw: egret.Tween;
    private sp: egret.Sprite;
    private noScaleX: number;
    private noScaleY: number;

    public constructor(bgName: string, titleName: string)
    {
        super();
        this.sp = new egret.Sprite();
        let sp: egret.Sprite = this.sp;
        this.addChild(sp);

        this.bg = ResourceUtils.createBitmapByName(bgName);
        let bg: egret.Bitmap = this.bg;
        sp.addChild(bg);

        this.title = ResourceUtils.createBitmapByName(titleName);
        let title = this.title;
        if (null == title.texture)
        {
            title.texture = RES.getRes(titleName);
        } 
        title.x = (this.bg.width - title.width) >> 1;//居中
        title.y = (this.bg.height - title.height) >> 1;//居中

        sp.addChild(title);
        this.noScaleX = sp.x;
        this.noScaleY = sp.y;
    }

    public setClick(fun: Function): void
    {
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickEvent, this);
        this.onClick = fun;
    }

    private onClickEvent(): void
    {
        if (GameData.isClickBtn) return;
        let sp: egret.Sprite = this.sp;
        let scaleX: number = (sp.width - sp.width * 0.8) / 2;
        let scaleY: number = (sp.height - sp.height * 0.8) / 2;
        this.tw = egret.Tween.get(this.sp);
        this.tw.to({"scaleX": 0.7, "scaleY": 0.7,
                    "x": scaleX, "y": scaleY}, 40).to({
                        "scaleX": 1,
                        "scaleY": 1,
                        "x": this.noScaleX,
                        "y": this.noScaleY
                    }, 40).call(this.onClickHandler, this);
    }

    private onClickHandler(): void
    {
        this.onClick();
    }
}